﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.CustomerPanel
{
    public partial class Booking : Page
    {
        private static List<PickBooking> _pickList = new List<PickBooking>();
        private static List<PickAndDropBooking> _pickAndDropList = new List<PickAndDropBooking>();

        protected void Page_Load(object sender, EventArgs e)
        {
            _pickList =
                PickBookingBLL.GetAllPickBookings(Convert.ToInt32(User.Identity.Name))
                    .OrderByDescending(item => item.OriginDateTime)
                    .ToList();
            _pickAndDropList =
                PickAndDropBookingBLL.GetAllPickAndDropBookings(Convert.ToInt32(User.Identity.Name))
                    .OrderByDescending(item => item.OriginDateTime)
                    .ToList();
            LoadGridview();
        }

        private void LoadGridview()
        {
            if (ddlType.SelectedValue.Equals("Pick"))
            {
                dgvBookings.DataSource = _pickList;
            }
            else
            {
                dgvBookings.DataSource = _pickAndDropList;
            }
            dgvBookings.DataBind();
        }

        protected void gvDrivers_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dgvBookings.PageIndex = e.NewPageIndex;
            LoadGridview();
        }

        protected void ibtnSearch_OnClick(object sender, ImageClickEventArgs e)
        {
            if (ddlType.SelectedValue.Equals("Pick"))
            {
                dgvBookings.DataSource = string.IsNullOrEmpty(tbSearch.Text)
                    ? _pickList
                    : _pickList.Where(b => b.BookingId == Convert.ToInt32(tbSearch.Text)).ToList();
                dgvBookings.DataBind();
            }
            else
            {
                dgvBookings.DataSource = string.IsNullOrEmpty(tbSearch.Text)
                    ? _pickAndDropList
                    : _pickAndDropList.Where(b => b.BookingId == Convert.ToInt32(tbSearch.Text)).ToList();
                dgvBookings.DataBind();
            }
        }

        protected void ddlType_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlType.SelectedValue.Equals("Pick"))
            {
                dgvBookings.DataSource = _pickList;
            }
            else
            {
                dgvBookings.DataSource = _pickAndDropList;
            }
            dgvBookings.DataBind();
            tbSearch.Text = "";
        }
    }
}